/*
 * =====================================================================================
 *
 *       Filename:  utils.cpp
 *
 *    Description:  utilities implementation
 *
 *        Version:  1.0
 *        Created:  01/01/12 21:41:37
 *       Revision:  none
 *       Compiler:  g++
 *
 *         Author:  LittleQ Su (), 98703060@nccu.edu.tw
 *        Company:  National Chengchi University, The Department of Computer Science
 *
 * =====================================================================================
 */
#include "utils.h"

void remove_dups (vector<int> & v) {
    sort(v.begin(), v.end());
    v.erase(unique(v.begin(), v.end()), v.end());
}

void print_vector (vector<int> & v) {
    for ( vector<int>::iterator i = v.begin() ; i < v.end() ; ++i ) {
        cout << (*i) << " ";
    }
    cout << endl;
}


bool has_key(const Datamap &the_map, const Datamap::key_type &the_key)
{
    Datamap::const_iterator iter = the_map.find(the_key);
    return iter != the_map.end();
}

void addtodatamap ( Datamap &datamap , string key , vector<string> &v) {
    if ( has_key ( datamap, key ) ) {
        Queryset target = datamap.find(key)->second;
        // the key already exists.
        for (vector<string>::iterator i = v.begin(); i < v.end() ; ++i) {
            target.push_back(StrToInt(*i));
        }

        remove_dups( target );
        datamap.erase(datamap.find(key));
        datamap.insert(pair<string, Queryset>(key, target));

    } else {
        // the key hasn't exist yet.
        Queryset target;
        for (vector<string>::iterator i = v.begin(); i < v.end(); ++i) {
            target.push_back(StrToInt(*i));
        }

        datamap.insert(pair<string, Queryset>(key, target));
    }
}



int StrToInt(string str){
	stringstream ss;
	int i = 0;
	ss << str;
	ss >> i;
	return i;
}

vector<string> &split(const string &s, char delim, vector<string> &elems) {
    stringstream ss(s);
    string item;

    while(getline(ss, item, delim)) {
        elems.push_back(item);
    }

    return elems;
}

vector<string> split(const string &s, char delim) {
    vector<string> elems;
    return split(s, delim, elems);
}
